package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcElectornicCheckDAO;
import com.trkj.dao.jpa.CcElectornicPrescribeDAO;
import com.trkj.dao.jpa.CcHospitalElectronicDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.IYzrPharStockDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.IJlCheckProjectDAO;
import com.trkj.pojos.CcElectornicCheck;
import com.trkj.pojos.CcElectornicPrescribe;
import com.trkj.pojos.CcHospitalElectronic;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.JlCheckProject;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrPharStock;
import com.trkj.vo.CcHospCheckVo;
import com.trkj.vo.CcHospitalElectronicVo;
import com.trkj.vo.HospEleVo;

@Service
@Transactional
public class CcHospitalElectronicService {

	@Autowired
	private CcHospitalElectronicDAO hospeleDAO;
	@Autowired
	private IYzrPharStockDAO yzrpharDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;
	@Autowired
	private CcPatientDAO patientDAO;
	@Autowired
	private CcElectornicPrescribeDAO celepreDAO;
	@Autowired
	private IJlCheckProjectDAO jlCheckDAO;
	@Autowired
	private CcElectornicCheckDAO ccEleCheckDAO;
	
	public Map<String, Object> queryAllYp(int page,int rows){
		Map<String, Object> map=new HashMap<>();
		PageRequest pr = new PageRequest(page - 1, rows);
		String pharStockStoreId ="YF-2017031900003";
		List<YzrPharStock> list= yzrpharDAO.findByPharStockStoreId(pr,pharStockStoreId);
		map.put("total", yzrpharDAO.findByPharStockcount(pharStockStoreId));
		map.put("rows", list);
		return map;
	}
	public List<YzrPharStock> queryLikeYp(String pharStockMedName){
		String pharStockStoreId ="YF-2017031900003";
		List<YzrPharStock> list=yzrpharDAO.findByPharStockMedName(pharStockStoreId, pharStockMedName);
		//System.err.println(list.size());
		return list;
	}
	public List<JlCheckProject> queryAllCheck(){
		return jlCheckDAO.queryAll();
	}
	public JlCheckProject queryOneCheck(long id){
		return jlCheckDAO.findOne(id);
	}
	public YzrPharStock queryBypharStockMedId(String pharStockMedId){
		String pharStockStoreId ="YF-2017031900003";
		return yzrpharDAO.findByPharpharStockMedId(pharStockStoreId, pharStockMedId);
	}
	public HospEleVo queryByEleId(BigDecimal eleId){
		HospEleVo hosp=new HospEleVo();
		hosp.setChospele(hospeleDAO.findOne(eleId.longValue()));
		hosp.setList(celepreDAO.queryByEleId(eleId));
		return hosp;
	}
	public List<CcElectornicPrescribe> queryByElepre(BigDecimal eleId){
		return celepreDAO.queryByEleId(eleId);
	}
	public List<CcElectornicCheck> queryByElepreCheck(BigDecimal eleId){
		return ccEleCheckDAO.queryByEleId(eleId);
	}
	public CcHospitalElectronic queryByOneEleId(BigDecimal eleId){
		return hospeleDAO.findOne(eleId.longValue());
	}
	public Map<String, Object> queryAllcchospele(int page,int rows,String docterid){
		Map<String, Object> map = new HashMap<>();
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcHospitalElectronic> list =hospeleDAO.findByheDoctorid(pr, new BigDecimal(docterid));
		map.put("total", hospeleDAO.findByheDoctoridCount(new BigDecimal(docterid)));
		map.put("rows", CcHospitalElectronicTOCcHospitalElectronicVo(list));
		return map;
	}
	public List<CcHospitalElectronicVo> queryBypatientName(int page,int rows,String pname){
		
		List<CcPatient> list=patientDAO.findBypname(pname);
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcHospitalElectronicVo> listVo=new ArrayList<CcHospitalElectronicVo>();
		for(CcPatient cp:list){
			List<CcHospitalElectronic> list2=hospeleDAO.findByheSickid(pr, new BigDecimal(cp.getPatientid()));
			listVo=CcHospitalElectronicTOCcHospitalElectronicVo(list2);
			
		}
		return listVo;
	}
	public List<CcHospitalElectronicVo> queryBypatienticid(int page,int rows,BigDecimal icid){
		CcPatient cp=patientDAO.findByOneCcPatient(icid);
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcHospitalElectronicVo> listVo=new ArrayList<CcHospitalElectronicVo>();
		if(cp!=null){
			List<CcHospitalElectronic> list2=hospeleDAO.findByheSickid(pr, new BigDecimal(cp.getPatientid()));
			listVo=CcHospitalElectronicTOCcHospitalElectronicVo(list2);
			
		}
		return listVo;
	}
	public void addHospEleVo(HospEleVo hospeleVo){
		CcHospitalElectronic chospele=new CcHospitalElectronic();
		CcHospitalElectronic chospele2=hospeleDAO.findOne(hospeleVo.getChospele().getHeId());
		if(chospele2!=null){
			chospele.setHeId(chospele2.getHeId());
		}
		chospele.setHeDate(new Date());
		chospele.setHeDoctorid(hospeleVo.getChospele().getHeDoctorid());
		chospele.setHeSickid(hospeleVo.getChospele().getHeSickid());
		chospele.setHeType(hospeleVo.getChospele().getHeType());
		chospele.setHospeleRemarks("未完成");
		hospeleDAO.save(chospele);
		List<CcElectornicPrescribe> list=hospeleVo.getList();
		List<CcElectornicPrescribe> list2=celepreDAO.queryByEleId(new BigDecimal(chospele.getHeId()));
		if(list2.size()>0){
			for(CcElectornicPrescribe cep2:list2){
				celepreDAO.delete(cep2.getEprescribeid());
			}
		}
		if(list.size()>0){
		for(CcElectornicPrescribe cep:list){
			CcElectornicPrescribe ccep= new CcElectornicPrescribe();
			ccep.setEleId(new BigDecimal(chospele.getHeId()));
			ccep.setDrugId(cep.getDrugId());
			CcElectornicPrescribe ccep2=celepreDAO.queryByEleIdandD(ccep.getEleId(), ccep.getDrugId());
			if(ccep2!=null){
				ccep2.setEdmenoy(ccep2.getEdmenoy().add(cep.getEdmenoy()));
				ccep2.setEpAmount(ccep2.getEpAmount().add(cep.getEpAmount()));
				celepreDAO.save(ccep2);
			}else{
				ccep.setPrescribeName(cep.getPrescribeName());
				ccep.setEdmenoy(cep.getEdmenoy());
				ccep.setEpAmount(cep.getEpAmount());
				celepreDAO.save(ccep);
			}
			
		}
		}
	}
	public void addCcHospCheckVo(CcHospCheckVo hospeleVo){
		CcHospitalElectronic chospele=new CcHospitalElectronic();
		CcHospitalElectronic chospele2=hospeleDAO.findOne(hospeleVo.getChospele().getHeId());
		if(chospele2!=null){
			chospele.setHeId(chospele2.getHeId());
		}
		chospele.setHeDate(new Date());
		chospele.setHeDoctorid(hospeleVo.getChospele().getHeDoctorid());
		chospele.setHeSickid(hospeleVo.getChospele().getHeSickid());
		chospele.setHeType(hospeleVo.getChospele().getHeType());
		chospele.setHospeleRemarks("未完成");
		hospeleDAO.save(chospele);
		List<CcElectornicCheck> list=hospeleVo.getList();
		List<CcElectornicCheck> list2=ccEleCheckDAO.queryByEleId(new BigDecimal(chospele.getHeId()) );
		if(list2.size()>0){
			for(CcElectornicCheck cep1:list2){
				ccEleCheckDAO.delete(cep1);
			}
		}
		for(CcElectornicCheck cep:list){
			CcElectornicCheck ccep= new CcElectornicCheck();
			ccep.setElecname(cep.getElecname());
			ccep.setEleTid(new BigDecimal(chospele.getHeId()));
			CcElectornicCheck ccep2=ccEleCheckDAO.queryByEleIdandD(ccep.getEleTid(), ccep.getElecname());
			if(ccep2==null){
				ccep.setEleCheckName(cep.getEleCheckName());
				ccep.setEtmenoy(cep.getEtmenoy());
				ccEleCheckDAO.save(ccep);
			}
		}
		
	}
	 
	public List<CcHospitalElectronicVo> CcHospitalElectronicTOCcHospitalElectronicVo
			(List<CcHospitalElectronic> list) {
			
		List<CcHospitalElectronicVo> listVo=new ArrayList<CcHospitalElectronicVo>();
		for(CcHospitalElectronic chospele:list){
			CcHospitalElectronicVo chospeleVo = new CcHospitalElectronicVo();
			chospeleVo.setHeDate(chospele.getHeDate());
			chospeleVo.setHeDoctorid(chospele.getHeDoctorid());
			chospeleVo.setHeId(chospele.getHeId());
			chospeleVo.setHeSickid(chospele.getHeSickid());
			chospeleVo.setHeType(chospele.getHeType());
			chospeleVo.setStaffid(chospele.getHeDoctorid().longValue());
			chospeleVo.setHospeleRemarks(chospele.getHospeleRemarks());
			if(chospele.getEleStaffid()!=null){
				chospeleVo.setEleStaffid(chospele.getEleStaffid());
				chospeleVo.setElestaffid(chospeleVo.getEleStaffid().longValue());
				Tbstaff staff2=staffDAO.findOne(chospeleVo.getElestaffid());
				chospeleVo.setElestaffname(staff2.getStaffName());
			}
			
			Tbstaff staff=staffDAO.findOne(chospeleVo.getStaffid());
			chospeleVo.setStaffname(staff.getStaffName());
			CcPatient cp=patientDAO.findOne(chospeleVo.getHeSickid().longValue());
			
			chospeleVo.setPatientid(cp.getPatientid());
			chospeleVo.setPname(cp.getPname());
			chospeleVo.setIcid(cp.getIcid());
			listVo.add(chospeleVo);
		}
		
		return listVo;
	}
}
